package Menu;

import Model.TimeThread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class RankInfo extends Menu {
    public JTextArea jTime = new JTextArea();         // 最快时间
    public JTextArea jWinStreak = new JTextArea();    // 最高连胜次数
    public JTextArea jWinAll = new JTextArea();       // 总胜利次数
    public JTextArea jWinRate = new JTextArea();      // 胜率

    /**
     * 初始化RankInfo
     *
     * <p>RankInfo是Menu的子类
     * 该界面为不同难度排行榜界面
     * 通过调用Menu的构造函数来构造RankInfo的实例
     * 显示排行榜信息的内容</p>
     *
     * @param record 要访问的排行榜信息文件路径
     */
    public RankInfo(String record) {
        super();
        File file = new File(record);
        String time = null;        // 速通纪录
        String winStreak = null;   // 连胜纪录
        String winNow = null;      // 当前连胜场次
        String winAll = null;      // 总胜利场次
        String timeAll = null;     // 总场次
        String winRate = null;     // 胜率
        if (file.length() == 0) {   // 文件初始化
            time = "尚未通关";
            winStreak = "0";
            winNow = "0";
            winAll = "0";
            timeAll = "0";
            winRate = "0";
        } else {
            try {
                FileReader fileReader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                time = bufferedReader.readLine();
                winStreak = bufferedReader.readLine();
                winNow = bufferedReader.readLine();
                winAll = bufferedReader.readLine();
                timeAll = bufferedReader.readLine();
                winRate = bufferedReader.readLine();
                bufferedReader.close();
                fileReader.close();
            } catch (IOException ioException) {
                ioException.getStackTrace();
            }
        }
        assert time != null;
        if (time.equals("尚未通关"))
            jTime.setText("最短通关时间：" + time);
        else
            jTime.setText("最短通关时间：" + TimeThread.transFormat(Long.parseLong(time)));
        jTime.setFont(new Font("Microsoft Yahei", Font.BOLD, 20));
        jTime.setBounds(70, 210, 230, 60);
        jTime.setLineWrap(true);
        jTime.setOpaque(false);
        jWinStreak.setText("最长连胜记录：" + winStreak);
        jWinStreak.setFont(new Font("Microsoft Yahei", Font.BOLD, 20));
        jWinStreak.setBounds(70, 280, 230, 60);
        jWinStreak.setLineWrap(true);
        jWinStreak.setOpaque(false);
        jWinAll.setText("总胜利场次：" + winAll);
        jWinAll.setFont(new Font("Microsoft Yahei", Font.BOLD, 20));
        jWinAll.setBounds(70, 350, 230, 60);
        jWinAll.setLineWrap(true);
        jWinAll.setOpaque(false);
        jWinRate.setText("胜率：" + winRate);
        jWinRate.setFont(new Font("Microsoft Yahei", Font.BOLD, 20));
        jWinRate.setBounds(70, 420, 230, 60);
        jWinRate.setLineWrap(true);
        jWinRate.setOpaque(false);
        super.add(jTime);
        super.add(jWinStreak);
        super.add(jWinAll);
        super.add(jWinRate);
        super.repaint();
    }

    /**
     * 绘制PrimaryRank的背景图片
     *
     * <p>图片使用images文件夹下的rank.png
     * 图片大小和窗口等大，面板容器设置为透明</p>
     */
    @Override
    public void paintBackground() {
        ImageIcon menuImage = new ImageIcon(this.getClass().getResource("/images/rank.png"));
        JLabel menuLabel = new JLabel(menuImage);
        //    添加图片并将其设置为窗口大小
        menuLabel.setBounds(0, 0, this.getWidth(), this.getHeight());
        //    将 menuLabel 置为容器最高层
        this.getLayeredPane().add(menuLabel, new Integer(Integer.MIN_VALUE));
        //    设置面板容器透明
        JPanel menuPanel = (JPanel) this.getContentPane();
        menuPanel.setOpaque(false);
    }

    /**
     * 通过用户点击的区域来相应不同事件
     *
     * <p>当用户点击返回时返回初始菜单</p>
     *
     * @param e 用户点击的区域
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        if (x >= 70 && x <= 300 && y >= 551 && y <= 597) {
            //    返回
            dispose();
            new Rank();
        }
    }
}
